package com.geek688.auxiliarytool.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang3.StringUtils;

import com.geek688.auxiliarytool.bean.FreeMarkBean;
import com.geek688.auxiliarytool.config.Config;
import com.geek688.auxiliarytool.util.LogUtil;
import com.geek688.auxiliarytool.util.PropertyUtil;

/**
 * freemark 自定义模板key-value
 * author geek
 * @version   V1.0
 */
public class FreeMarkSettingUI extends JFrame implements ActionListener{
	
	private double WIDTH = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
	private double HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
	
	private static final long serialVersionUID = 1L;
	private JTextField urlField,keyField,valueField,descriptionField;
	
	private boolean enableFlag;
	private static FreeMarkSettingUI freeMarkSettingUI = null;
	private static JTextArea area = null;
	/**
	 * 绘制界面
	 * @author Jack
	 */
	private FreeMarkSettingUI() {
		this.setLocation(new Point((int) (WIDTH / 2) -175, (int) (HEIGHT / 2)-150));
        this.setSize(350, 300);  
        this.setTitle("设置"); 
        this.setLayout(new FlowLayout(FlowLayout.CENTER));

        JPanel pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pan.setPreferredSize(new Dimension(300, 70));
		JPanel sqlPanel = new JPanel(new GridLayout(4, 2, 0, 5));
        sqlPanel.setPreferredSize(new Dimension(300, 120));
        JLabel url = new JLabel("外部配置文件路径：");
        urlField = new JTextField("",35);
        urlField.setPreferredSize(new Dimension(300, 25));
        JLabel key = new JLabel("自定义key：");
        keyField = new JTextField();
        JLabel value = new JLabel("自定义value：");
        valueField = new JTextField();
        JLabel description = new JLabel("描述：");
        descriptionField = new JTextField();
        JButton setBtn = new JButton("添 加");
        setBtn.setActionCommand("set");setBtn.addActionListener(this);
        JButton detailBtn = new JButton("查 看");
        detailBtn.setActionCommand("detail");detailBtn.addActionListener(this);
        JButton clearBtn = new JButton("清 空");
        clearBtn.setActionCommand("clear");clearBtn.addActionListener(this);
        pan.add(url);pan.add(urlField);
        this.add(pan);
        
        sqlPanel.add(key);sqlPanel.add(keyField);
        sqlPanel.add(value);sqlPanel.add(valueField);
        sqlPanel.add(description);sqlPanel.add(descriptionField);
        
        this.add(sqlPanel);
        JPanel btnpan = new JPanel(new FlowLayout(FlowLayout.LEFT));
        btnpan.add(setBtn);btnpan.add(detailBtn);btnpan.add(clearBtn);
        this.add(btnpan);
        this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("set")) {
			String url = urlField.getText().trim().replaceAll("(：)|(:)", ":").replaceAll("\\s+", "");
			String key = keyField.getText().trim().replaceAll("\\s+", "");
			String value = valueField.getText().trim().replaceAll("\\s+", "");
			String description = descriptionField.getText().trim();
			if (StringUtils.isNotBlank(url)) {
				PropertyUtil propertyUtil = new PropertyUtil(url);
				Properties prop = propertyUtil.getProperties();
				LinkedList<String> propertyNames = new LinkedList<>();
				
				Enumeration<?> en = prop.propertyNames();
				while(en.hasMoreElements()){
					String propertyName = (String) en.nextElement();
					propertyNames.add(propertyName);
				}
				area.append("开始扫描自定义键值对"+Config.CRLF+Config.CRLF);
				for(String propertyName:propertyNames){
					String v = (String) prop.get(propertyName);
					area.append(propertyName+":"+v+Config.CRLF);
					LogUtil.writeFreeMarkSet(propertyName, v.trim(), "");
				}
				area.append("添加自定义键值对成功！"+Config.CRLF+Config.CRLF);
			}
			LogUtil.writeFreeMarkSet(key, value, description);
		}
		if (e.getActionCommand().equals("clear")) {
			LogUtil.clearLog("freemark");
			//读取自定义参数键值对
	        List<FreeMarkBean> readLog = LogUtil.readFreeMarkSet();
	        area.setText("");
	        area.append("自定义参数:"+Config.CRLF);
	        for(FreeMarkBean markBean: readLog){
	        	area.append(markBean.getKey()+" : "+markBean.getValue()+Config.CRLF);
	        }
		}
		if (e.getActionCommand().equals("detail")) {
			//读取自定义参数键值对
	        List<FreeMarkBean> readLog = LogUtil.readFreeMarkSet();
	        area.setText("");
	        area.append("自定义参数:"+Config.CRLF);
	        for(FreeMarkBean markBean: readLog){
	        	if(markBean.getKey()!=null && !"".equals(markBean.getKey())) {
	        		area.append(markBean.getKey()+" : "+markBean.getValue()+Config.CRLF);
	        	}
	        }
		}
	}

	public boolean isEnableFlag() {
		return enableFlag;
	}
	
	public static FreeMarkSettingUI getInstance(JTextArea area) {
		FreeMarkSettingUI.area = area;
		if (freeMarkSettingUI == null) {
			freeMarkSettingUI = new FreeMarkSettingUI();
		}
		freeMarkSettingUI.setVisible();
		return freeMarkSettingUI;
	}
	public void setVisible() {
		this.setVisible(true);
	}
}
